//
//  InputUserDetailViewController.m
//  QuickCure1
//
//  Created by Oopsjesse on 05/11/2016.
//  Copyright © 2016 maoge. All rights reserved.
//

#import "InputUserDetailViewController.h"

#import "ProvinceAndCityTableView.h"
#import "Users.h"
#import "NSString+Path.h"

@interface InputUserDetailViewController ()<UIPickerViewDataSource,UIPickerViewDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@property (weak, nonatomic) IBOutlet UIPickerView *genderPicker;
@property(nonatomic,strong)NSArray<NSString *> *pickerViewDataList;
@property (weak, nonatomic) IBOutlet UITextField *textFieldUserID;
@property (weak, nonatomic) IBOutlet UITextField *textfieldPassword;
@property (weak, nonatomic) IBOutlet UITextField *textFieldName;
@property (weak, nonatomic) IBOutlet UITextField *textFieldAge;
@property (weak, nonatomic) IBOutlet UILabel *labelProvince;
@property (weak, nonatomic) IBOutlet UILabel *labelCity;
@property (weak, nonatomic) IBOutlet UITextField *textFieldTelNum;
@property (weak, nonatomic) IBOutlet UITextField *labelWeight;
@property (weak, nonatomic) IBOutlet UITextField *labelHeight;

@property (weak, nonatomic) IBOutlet UIButton *buttonCommit;

@property (weak, nonatomic) IBOutlet UIImageView *imageViewUserHead;

@property(nonatomic,copy)NSString *gender;



@end

@implementation InputUserDetailViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    [self setupUI];
}



-(void)setupUI{

    _genderPicker.showsSelectionIndicator = YES;  //显示指示器
    
    _genderPicker.dataSource = self;  //数据源//
    
    _genderPicker.delegate = self;  //代理
    
    _pickerViewDataList = @[@"老司机",@"人妖",@"男",@"女"];
    
   
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapToResignFirstResponder)];
    
    [self.view addGestureRecognizer:tap];

    UITapGestureRecognizer *tapImageView = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickToSaveImage:)];
    
    [self.imageViewUserHead addGestureRecognizer:tapImageView];
    
   
    

}


- (void)clickToSaveImage:(UIButton *)sender {
    
    //实例化控制器
    UIImagePickerController *pickerVC = [[UIImagePickerController alloc] init];
    
    //设置获取图片的源
    pickerVC.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    
    //设置代理
    pickerVC.delegate = self;
    
    //模态控制器
    [self presentViewController:pickerVC animated:YES completion:nil];
    
}

#pragma mark -
#pragma mark - pickerVC代理方法
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    
    //拿到元始的图片对象
    _imageViewUserHead.image = info[UIImagePickerControllerOriginalImage];
    
    //获取沙盒路径
    NSString *cachePath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject;
    
    //拿到图片的url，并转换为string
    NSString *imageName = ((NSURL *)info[UIImagePickerControllerReferenceURL]).absoluteString;
    
    //Md5处理
    imageName = [imageName cz_md5String];

    //拼接图片存储路径
    NSString *filePath = [cachePath stringByAppendingPathComponent:imageName];
    
#warning 可能图片保存的不对
    //转换图片对象为二进制
    NSData *data = UIImagePNGRepresentation(_imageViewUserHead.image);
    
    //存储路径赋值
    _model.head_photo = filePath;
    
    //写入沙盒
    [data writeToFile:filePath atomically:YES];

    //dismiss控制器
    [picker dismissViewControllerAnimated:YES completion:nil];
    
}




- (IBAction)clickToChooseProvinceAndCity:(UIButton *)sender {
    
    ProvinceAndCityTableView *tableView = [[ProvinceAndCityTableView alloc] init];
    
    tableView.callBackBlock = ^(NSString *province,NSString *city){
    
        _labelProvince.text = province;
        _labelCity.text = city;
    
    };
    
    [self.navigationController pushViewController:tableView animated:YES];
    
}

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    
    _gender = _pickerViewDataList[row];
}

- (void)tapToResignFirstResponder{

    [self.view.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        [obj resignFirstResponder];
    }];

}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{

    return YES;
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{

    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{

    return _pickerViewDataList.count;

}


- (nullable NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component{

    NSAttributedString *attributedString = [[NSAttributedString alloc]initWithString:_pickerViewDataList[row] attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:5]}];
    
    return attributedString;

}


- (IBAction)clickToSaveUserInfo:(UIButton *)sender {
    
    _model.true_name = _textFieldName.text;
    
    _model.account = _textFieldUserID.text;
    
    _model.province = _labelProvince.text;
    
    _model.city = _labelCity.text;
    
    _model.weight = @(_labelWeight.text.integerValue);
    
    _model.height = @(_labelHeight.text.integerValue);
    
    _model.gender = _gender;
    
    _model.age = @(_textFieldAge.text.integerValue);
    
    _model.mobile_number = @(_textFieldTelNum.text.integerValue);
    
    _model.easymob_password = [_textfieldPassword.text cz_md5String];
    
    NSMutableArray *array;
    
    array = [[NSKeyedUnarchiver unarchiveObjectWithFile:kUsersInfoPath] mutableCopy];
    
    if (array == nil) {
        
        array = [NSMutableArray array];
    }
    
    [array addObject:_model];
    
    [NSKeyedArchiver archiveRootObject:array.copy toFile:kUsersInfoPath];
    
    [self.navigationController popToRootViewControllerAnimated:YES];
    
    
}



@end
